package com.example.calendarapp;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.sql.DataSource;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import android.util.Log;
import android.view.View.OnClickListener;

/**
 * month adapter class for month grid view
 * @author nathan
 *
 */

public class MonthViewAdapter extends BaseAdapter {
	private static String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	public static Calendar c = Calendar.getInstance();
	private static int[] mValue = {0,3,3,6,1,4,6,2,5,0,3,5};
	private EventsDataSource datasource;
	private static int[] mLeapYearValue = {6,2,3,6,1,4,6,2,5,0,3,5};
	int offset, height, num;
	Context context;
	
	
	
	/**
	 * Constructor from context, open database
	 * @param ctx
	 */
	public MonthViewAdapter(Context ctx)
	{
		this.datasource = EventsDataSource.getInstance(ctx);
		datasource.createDatabase();
		datasource.open();
		this.context = ctx;
	}

	/**
	 * getNumber of days in month, uses a simple formula that can be found on wikipedia
	 */
	@Override
	public int getCount() {
		if(c.get(Calendar.YEAR)%4 == 0 && c.get(Calendar.YEAR)%100 != 0)
			offset = (1+mLeapYearValue[c.get(Calendar.MONTH)]+c.get(Calendar.YEAR)%100 + (c.get(Calendar.YEAR)%100)/4 + 6-(2*((c.get(Calendar.YEAR)/100)%4)))%7;
		else
			offset = (1+mValue[c.get(Calendar.MONTH)]+c.get(Calendar.YEAR)%100 + (c.get(Calendar.YEAR)%100)/4 + 6-(2*((c.get(Calendar.YEAR)/100)%4)))%7;
		int size = offset + c.getActualMaximum(Calendar.DAY_OF_MONTH);
		if(size%7 != 0)
		{
			size += 7 - size%7;
		}
		this.num = size;
		return size;
	}

	
	/**
	 * getItem not used had to be implemented though
	 */
	@Override
	public Object getItem(int arg0) {
		
		return arg0;
	}

	/**
	 * getItemId also had to be implemented, also not used
	 */
	
	@Override
	public long getItemId(int arg0) {

		return arg0;
	}

	/**
	 * increment the calendar
	 */
	
	public void incrementMonth(){
		c.add(Calendar.MONTH, 1);
	}
	
	/**
	 * decrement the calendar
	 */
	
	public void decrementMonth(){
		c.add(Calendar.MONTH, -1);
	}
	
	/**
	 * set height of gridview items, used for dynamic sizing of gridView
	 * @param h
	 */
	
	public void setHeight(int h)
	{
		this.height = h;
	}
	/**
	 *view inflater method, inflates view based on position and number of items in month
	 *
	 *@param position
	 *@param parent
	 *@param viewgroup
	 *@return v
	 */
	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View convertView = inflater.inflate(R.layout.monthitem, null);
		convertView.setMinimumHeight(height/(num/7));
		TextView date, numEvents;
		date = (TextView) convertView.findViewById(R.id.date);
		numEvents = (TextView) convertView.findViewById(R.id.numEvents);
		if(arg0<offset || arg0 - offset >= c.getActualMaximum(Calendar.DAY_OF_MONTH))
		{
			convertView.setBackground(context.getResources().getDrawable(R.drawable.notmonthbg));
			date.setVisibility(4);
			numEvents.setVisibility(4);
		}
		else{
			if(arg0%7 == 0 || arg0%7 == 6) convertView.setBackground(context.getResources().getDrawable(R.drawable.weekendbg));
			else convertView.setBackground(context.getResources().getDrawable(R.drawable.weekdaybg));
			date.setText(Integer.toString(arg0-offset + 1));
			numEvents.setText(Integer.toString(datasource.getNumEvents(c.get(Calendar.YEAR), c.get(Calendar.MONTH), arg0-offset+1, arg0%7)));
		}
		
		convertView.setTag(arg0);
		
		
		return convertView;	
	}
	
	/**
	 * format datestring
	 * @param position
	 * @return datestring
	 */
	
	
	public String getDateString(int position)
	{
		if(position >= offset)
		{
			return String.valueOf(c.get(Calendar.YEAR)) + "-" + String.valueOf(c.get(Calendar.MONTH)+1) + "-" + String.valueOf(position-offset+1);
		}
		else
			return "";
	}
	
	/**
	 * close the database
	 */
	
	public void close()
	{
		datasource.close();
	}
}
